<!--
 * @Author: your name
 * @Date: 2022-03-23 23:44:26
 * @LastEditTime: 2022-03-24 11:04:18
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \算法测试\day0906\深拷贝.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let obj = {
            a: 123,
            b: 'str1',
            c: {
                st1: 'ui1',
                str2: [7, 8, {
                    num: 789
                }],
                str3: {
                    op1: 1,
                    op: 2
                }
            },
            d: [10, 20, 30, {
                rr: 'ggg',
                tt: [9, 9, 9]
            }]
        }

        const deepLoop = function (obj, map = new WeakMap()) {
            if (typeof obj === 'object') {
                let newobj = obj instanceof Array ? [] : {};
                //判断是否已经Clone过 直接返回
                if (map.get(obj)) return map.get(obj);
                //没Clone 记录
                map.set(obj, newobj);
                for (const key in obj) {
                    newobj[key] = deepLoop(obj[key]);
                }
                return newobj;
            } else {
                return obj;
            }
        }
        let obj2 = deepLoop(obj);
        obj2.c.str2[2].num = 9999;
        console.log(obj);
        console.log(obj2);


        
    </script>
</body>

</html>